import { createOrder, addrressList, createOrderFromCart } from '../../../../common/api/index'
Page({
    data: {
		remark: null,
		goodsInfo: [],//下单商品
		addrressInfo: {},//收货信息
		totalMoney: 0,
		fromType: null,//记录是什么方式下单的
    },
    onShow() {
		let info = wx.getStorageSync('goodsInfo')//获取下单商品信息
		console.log(info,info instanceof Array,'================')
		if(info.length > 0){
			let totalMoney = 0
			info.forEach(item => {
				totalMoney = Number(totalMoney) + (Number(item.buyCount)*Number(item.price))
			})
			this.setData({
				goodsInfo: info,
				totalMoney: totalMoney
			})
			console.log(info,'==============================下单商品')
		}
		let addrress = wx.getStorageSync('chooeseAddrressData')//获取选择的地址
		if(addrress.receiveId){
			this.setData({
				addrressInfo: addrress
			})
			console.log(addrress,'==============================收货地址')
		}else{
			this.getAddrressList()//加载收获地址列表
		}
    },
    onLoad(options) {
		if(options.type == 'cart'){
			this.setData({
				fromType: options.type
			})
		}
	},
	// 获取默认收货地址
	getAddrressList(){
		addrressList().then(res => {
			if(res.code == 200){
				if(res.data.length > 0){
					res.data.forEach(item => {
						if(item.defaultFlag){
							this.setData({
								addrressInfo: item
							})
						}
					})
				}
			}
		})
	},
	// 去选择地址
	toAddrress(){
		wx.navigateTo({
			url: '/pages/component/mine/addrress/index/index'
		})
	},
	// 备注
	remarkChange(e){
		this.setData({
			remark: e.detail
		})
	},
	//创建订单并支付
	toSubmit(){
		// addrressInfo
		// goodsInfo
		if(!this.data.addrressInfo.receiveId){
			wx.showToast({
				title: '请选择收获地址',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(!this.data.fromType){//从商品详情 进入订单提交
			let data = {
				skuId: this.data.goodsInfo[0].id,
				receiveId: this.data.addrressInfo.receiveId,
				remark: this.data.remark,
				buyCount: this.data.goodsInfo[0].buyCount,
			}
			createOrder(data).then(res => {
				if(res.code == 200){
					let path = '/pages/component/classify/pay/index?orderId='+res.data+'&productIds='+this.data.goodsInfo[0].productId+"&totalMoney="+ this.data.totalMoney
					wx.$goTo(path,true)
				}
			})
		}else{//从购物车  进入订单提交
			let arr = []
			this.data.goodsInfo.forEach(item => {
				let obj = {
					cardId: item.cartId,
					buyCount: item.buyCount,
				}
				arr.push(obj)
			})
			let data = {
				receiveId: this.data.addrressInfo.receiveId,
				remark: this.data.remark,
				shopCartInfo: arr
			}
			createOrderFromCart(data).then(res => {
				if(res.code == 200){
					let path = '/pages/component/classify/pay/index?orderId='+res.data+'&productIds='+this.data.goodsInfo[0].productId+"&totalMoney="+ this.data.totalMoney
					wx.$goTo(path,true)
				}
			})
		}
	},
	// 改变数量
	onChangeNum(e){
		let index = e.currentTarget.dataset.index;
		let num = e.detail;
		this.data.goodsInfo[index].buyCount = num;
		let totalMoney = 0
		this.data.goodsInfo.forEach(item => {
			totalMoney = Number(totalMoney) + (Number(item.buyCount)*Number(item.price))
		})
		this.setData({
			totalMoney: totalMoney
		})
		wx.setStorageSync('goodsInfo', this.data.goodsInfo)
	},
})